package cndw.core.dispatch;

import java.io.Serializable;
import java.net.InetSocketAddress;

/**
 * @author zuojie.x
 */
public class DefaultRequest implements Request, Serializable {
	private static final long serialVersionUID = 1L;
	private Session session;
	private int actionId;
	private Serializable[] params;
	private String sign;
	
	protected DefaultRequest(
			Session session,
			long requestTime,
			int actionId,
			Serializable[] params,
			String sign) {
		this.session = session;
		this.actionId = actionId;
		this.params = params != null ? params : new Serializable[0];
		this.sign = sign;
	}
	
	@Override
	public String getPlayerId() {
		return session.getPlayerId();
	}
	
	@Override
	public Session getSession() {
		return session;
	}
	
	/**
	 * 设置此次连接的会话
	 * @param session
	 */
	public void setSession(Session session) {
		this.session = session;
	}

	@Override
	public String getHostAddress() {
		return ((InetSocketAddress) session.getChannel().localAddress()).getAddress().getHostAddress();
	}

	@Override
	public int getActionId() {
		return actionId;
	}

	@Override
	public Serializable[] getParams() {
		return params;
	}
	
	void setSign(String sign) {
		this.sign = sign;
	}
	
	@Override
	public String getSign() {
		return sign;
	}

	@Override
	public String toString() {
		int i = 0;
		String desc = "";
		for (Serializable serial : params) {
			if (i ++ > 0)
				desc += ",";
			desc += serial.getClass().getSimpleName();
		}
		return "Request [address=" + getHostAddress() + ", actionId=" + actionId + ", params=(" + desc + ")";
	}
}
